package com.llg.service;

import com.llg.mapper.LedMatchReceiveMapper;
import com.llg.mapper.LedSpecMapper;
import com.llg.mapper.ReceiveSpecMapper;
import com.llg.mapper.SupplySpecMapper;
import com.llg.model.ComputeModel;
import com.llg.model.code.CommonCode;
import com.llg.model.code.ComputeCode;
import com.llg.model.response.DataResult;
import com.llg.pojo.LedMatchReceive;
import com.llg.pojo.LedSpec;
import com.llg.pojo.ReceiveSpec;
import com.llg.pojo.SupplySpec;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

@Service
public class ComputeService {

    @Autowired
    private LedSpecMapper specMapper;
    @Autowired
    private SupplySpecMapper supplySpecMapper;
    @Autowired
    private ReceiveSpecMapper receiveSpecMapper;
    @Autowired
    private LedMatchReceiveMapper ledMatchReceiveMapper;

    /**
     * @Description: 通过面积计算
     * @param:  specId:型号id
     * @param:  area:面积
     * @param:  mode:计算方式，1=16:9  2=4:3
     * @Author:         llg
     * @CreateDate:     2020/3/18 20:01
     */
    public DataResult<ComputeModel> computeByArea(Integer specId, Double area, Integer mode){
        if(specId == null) return new DataResult<>(ComputeCode.CODE1, null);
        if(area <= 0) return new DataResult<>(ComputeCode.CODE4, null);
        if(mode != 1 && mode != 2) return new DataResult<>(ComputeCode.CODE3, null);
        //获取型号信息
        LedSpec spec = specMapper.selectByPrimaryKey(specId);
        if(spec == null) return new DataResult<>(ComputeCode.CODE2, null);
        ComputeModel computeModel = new ComputeModel();
        if(mode.equals(1)){
            //按照16：9计算
            double x = Math.sqrt(area * 16 / 9);
            double y = x * 9 / 16;
            //计算实际宽高、宽高各占单元板数以及总单元板数
            basicCompute(x,y,spec,computeModel);
        }else{
            //按照4：3计算
            double x = Math.sqrt(area * 4 / 3);
            double y = x * 3 / 4;
            //计算实际宽高、宽高各占单元板数以及总单元板数
            basicCompute(x,y,spec,computeModel);
        }
        return new DataResult<>(CommonCode.SUCCESS, computeModel);
    }

    /**
     * @Description:  通过宽高计算
     * @param:  specId:型号id
     * @param:  width:宽
     * @param:  height:高
     * @param:  mode:计算方式，1=16:9  2=4:3
     * @Author:         llg
     * @CreateDate:     2020/3/18 20:06
     */
    public DataResult<ComputeModel> computeByLong(Integer specId,Double width,Double height,Integer mode){
        if(specId == null) return new DataResult<>(ComputeCode.CODE1, null);
        if(width <= 0) return new DataResult<>(ComputeCode.CODE5, null);
        if(height <= 0) return new DataResult<>(ComputeCode.CODE6, null);
        if(mode != 1 && mode != 2) return new DataResult<>(ComputeCode.CODE3, null);
        //获取型号信息
        LedSpec spec = specMapper.selectByPrimaryKey(specId);
        if(spec == null) return new DataResult<>(ComputeCode.CODE2, null);
        ComputeModel computeModel = new ComputeModel();
        if(mode.equals(1)){
            //按照16：9计算
            //计算实际宽高、宽高各占单元板数以及总单元板数
            basicCompute(width,height,spec,computeModel);
        }else{
            //按照4：3计算
            //计算实际宽高、宽高各占单元板数以及总单元板数
            basicCompute(width,height,spec,computeModel);
        }
        return new DataResult<>(CommonCode.SUCCESS, computeModel);
    }

    /**
     * @Description:  根据单元板总数和电源型号id计算需要多少个电源,
     * 计算公式：电源个数 = （单元板总数 * 单元板最大电流） / （电源电流 * 0.8），上取整
     * @param:  num:单元板总数
     * @param:  lid:led型号id
     * @param:  sid:电源型号id
     * @Author:         llg
     * @CreateDate:     2020/3/19 21:35
     */
    public DataResult<SupplySpec> computeSupplyNum(Integer num,Integer lid,Integer sid){
        if(num == null || num <= 0) return new DataResult<>(CommonCode.FAIL,null);
        //获取电源型号信息
        SupplySpec supplySpec = supplySpecMapper.selectByPrimaryKey(sid);
        //获取led型号信息
        LedSpec ledSpec = specMapper.selectByPrimaryKey(lid);
        //计算电源个数
        String[] split = ledSpec.getElectric().split("≤");
        int supplyNum = (int)Math.ceil((num * Double.parseDouble(split[1])) / (supplySpec.getElectric() * 0.8));
        supplySpec.setNum(supplyNum);
        return new DataResult<>(CommonCode.SUCCESS, supplySpec);
    }

    /**
    * @Description:  计算接收卡张数
    *  计算方式：（屏幕宽密度/接收卡宽密度）上取整 * （屏幕高密度/接收卡高密度）上取整
    * @param:      densityRatio：屏幕像素比
    * @param:      lid:led的id
    * @param:      rid:接收卡id
    * @Author:         llg
    * @CreateDate:     2020/3/22 18:47
    */
    public DataResult<ReceiveSpec> computeReceiveNum(String densityRatio,int lid,int rid){
        //获取接收卡信息
        ReceiveSpec receiveSpec = receiveSpecMapper.selectByPrimaryKey(rid);
        Example example = new Example(LedMatchReceive.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("lid",lid);
        criteria.andEqualTo("rid",rid);
        LedMatchReceive ledMatchReceive = ledMatchReceiveMapper.selectOneByExample(example);
        //获取屏幕和接收卡高宽密度
        String[] split = densityRatio.split("\\*");
        int screenWidth = Integer.parseInt(split[0]);
        int screenHeight = Integer.parseInt(split[1]);
        String[] split1 = ledMatchReceive.getCarryMode2().split("\\*");
        int receiveWidth = Integer.parseInt(split1[0]);
        int receiveHeight = Integer.parseInt(split1[1]);
        //计算接收卡数量
        int num = (int) ((Math.ceil(screenWidth / receiveWidth)) * (screenHeight/receiveHeight));
        receiveSpec.setNum(num);
        return new DataResult<>(CommonCode.SUCCESS, receiveSpec);
    }


    /**
    * @Description:  以粗略宽计算实际宽以及宽有多少个单元板和总单元板数量等
    * @param:  width:粗略宽
    * @param:  height:粗略高
    * @param:  size:单元板宽尺寸
    * @Author:         llg
    * @CreateDate:     2020/3/18 22:37
    */
    private void basicCompute(double width, double height, LedSpec spec, ComputeModel computeModel){
        //获取单元板宽高
        String[] split = spec.getSize().split("\\*");
        double bitWidth = Integer.parseInt(split[0]) / 1000.0;
        double bitHeight = Integer.parseInt(split[1]) / 1000.0;
        //计算宽高方向各占多少个单元板
        int widthNum = (int)(width/bitWidth);
        int heightNum = (int)(height/bitHeight);
        computeModel.setWidthNum(widthNum);
        computeModel.setHeightNum(heightNum);
        //计算实际宽、高、面积
        double actualWidth = widthNum * bitWidth;
        double actualHeight = heightNum * bitHeight;
        double area = actualHeight * actualWidth;
        computeModel.setWidth(actualWidth);
        computeModel.setHeight(actualHeight);
        computeModel.setArea(area);
        //计算单元板总数量
        int total = widthNum * heightNum;
        computeModel.setNum(total);
        //获取单元板密度
        String[] split1 = spec.getResPower().split("\\*");
        int bitWidthRes = Integer.parseInt(split1[0]);
        int bitHeightRes = Integer.parseInt(split1[1]);
        //计算长宽密度比
        String densityRatio = (bitWidthRes * widthNum) + "*" + (bitHeightRes * heightNum);
        computeModel.setDensityRatio(densityRatio);
        //计算总密度
        int totalDensity = (bitWidthRes * widthNum) * (bitHeightRes * heightNum);
        computeModel.setTotalDensity(totalDensity);

    }
}
